package com.sk.sk_frame_cloud.config;

import com.sk.sk_frame_cloud.filter.AuthFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;

import javax.annotation.Resource;

/**
 * @Author jiaok
 * @Date 2024-01-15 18:06
 */
@Configuration
public class SKGatewayConfigration {
    @Resource
    private AuthFilter authFilter;
    @Bean
    @Order
    public RouteLocator customizedRoutes(RouteLocatorBuilder builder){

        return builder.routes()
                .route(r -> r.path("/api/auth/**").and().method(HttpMethod.POST)
                        //请求头要包含的字段
                        .and().header("Auth")
                        .filters(f -> f.rewritePath("   /api/(?<segment>/?.*)","/$\\{segment}").filter(authFilter))
                        .uri("lb://sk-frame-auth")).build();

    }
}
